package com.jianda.view;

import com.alibaba.fastjson.JSON;
import com.jianda.commons.content.Content;
import com.jianda.entity.dto.SerialPostDto;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 文本框视图类
 */
public class TextView{
    static JTextField serialNumberField;
    static JTextField baudRateField;
    static JTextField checkoutBitField;
    static JTextField dataBitField;
    static JTextField stopBitField;

   // private static TextView textView;
   // private static JFrame frame;

    /*public static TextView getInstance() {
     //   frame = jFrames;
        if (textView == null) {
            synchronized (TextView.class) {
                if (textView == null) {
                    textView = new TextView();
                }
            }
        }
        return textView;
    }*/

    /*@Override
    protected void printComponent(Graphics g) {
        super.paintComponent(g);
        int x = (int)(frame.getWidth() * 0.012);
        int y = (int)(frame.getHeight() * 0.024);
        int width = (int)(frame.getWidth() * 0.0387);
        int height = (int)(frame.getHeight() * 0.019);
        System.out.println("jFrame.getWidth()/text:" + frame.getWidth());
        System.out.println("jFrame.getHeight()/text:" + frame.getHeight());
        Font font = new Font("Dialog", 4, 20);

        JLabel labelA = new JLabel("a波形");
        labelA.setBounds(x, y, width, height);
        labelA.setFont(font);

        JLabel labelB = new JLabel("b波形");
        labelB.setBounds(x, (int)(y + intervalText), width, height);
        labelB.setFont(font);

        JLabel labelC = new JLabel("c波形");
        labelC.setBounds(x,  (int)(y + (intervalText * 2)), width, height);
        labelC.setFont(font);

        JLabel labelD = new JLabel("d波形");
        labelD.setBounds(x,  (int)(y + (intervalText * 3)), width, height);
        labelD.setFont(font);

        JLabel labelE = new JLabel("e波形");
        labelE.setBounds(x,  (int)(y + (intervalText * 4)), width, height);
        labelE.setFont(font);

        JLabel labelF = new JLabel("f波形");
        labelF.setBounds(x,  (int)(y + (intervalText * 5)), width, height);
        labelF.setFont(font);

        JLabel labelG = new JLabel("g波形");
        labelG.setBounds(x,  (int)(y + (intervalText * 6)), width, height);
        labelG.setFont(font);

        JLabel labelH = new JLabel("h波形");
        labelH.setBounds(x,  (int)(y + (intervalText * 7)), width, height);
        labelH.setFont(font);

        JLabel labelI = new JLabel("i波形");
        labelI.setBounds(x,  (int)(y + (intervalText * 8)), width, height);
        labelI.setFont(font);

        JLabel labelJ = new JLabel("j波形");
        labelJ.setBounds(x,  (int)(y + (intervalText * 9)), width, height);
        labelJ.setFont(font);

        frame.getContentPane().add(labelA);
        frame.getContentPane().add(labelB);
        frame.getContentPane().add(labelC);
        frame.getContentPane().add(labelD);
        frame.getContentPane().add(labelE);
        frame.getContentPane().add(labelF);
        frame.getContentPane().add(labelG);
        frame.getContentPane().add(labelH);
        frame.getContentPane().add(labelI);
        frame.getContentPane().add(labelJ);
    }*/

    /**
     * 设置波形名称
     */
    public static void editText(JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) screenSize.getWidth();
        int screenHeight = (int) screenSize.getHeight();
       /* int x  = 22;
        int y  = 25;
        int height = 25;
        int width = 90;*/
        int x = (int)(screenWidth * 0.0115);
        int y = (int)(screenHeight * 0.023);
        int width = (int)(screenWidth * 0.047);
        int height = (int)(screenHeight * 0.023);

        int intervalText = (int)(screenHeight * 0.092);
        System.out.println("screenWidth:" + screenWidth);
        System.out.println("screenHeight:" + screenHeight);
        Font font = new Font("Dialog", 2, 15);

        JLabel labelA = new JLabel("a波形:");
        labelA.setBounds(x, y + 10, width, height);
        labelA.setFont(font);

        JLabel labelB = new JLabel("b波形:");
        labelB.setBounds(x, (int)(y + intervalText), width, height);
        labelB.setFont(font);

        JLabel labelC = new JLabel("c波形:");
        labelC.setBounds(x,  (int)(y + (intervalText * 2)), width, height);
        labelC.setFont(font);

        JLabel labelD = new JLabel("d波形:");
        labelD.setBounds(x,  (int)(y + (intervalText * 3)), width, height);
        labelD.setFont(font);

        JLabel labelE = new JLabel("e波形:");
        labelE.setBounds(x,  (int)(y + (intervalText * 4)), width, height);
        labelE.setFont(font);

        JLabel labelF = new JLabel("f波形:");
        labelF.setBounds(x,  (int)(y + (intervalText * 5)), width, height);
        labelF.setFont(font);

        JLabel labelG = new JLabel("g波形:");
        labelG.setBounds(x,  (int)(y + (intervalText * 6)), width, height);
        labelG.setFont(font);

        JLabel labelH = new JLabel("h波形:");
        labelH.setBounds(x,  (int)(y + (intervalText * 7)), width, height);
        labelH.setFont(font);

        JLabel labelI = new JLabel("i波形:");
        labelI.setBounds(x,  (int)(y + (intervalText * 8)), width, height);
        labelI.setFont(font);

        JLabel labelJ = new JLabel("j波形:");
        labelJ.setBounds(x,  (int)(y + (intervalText * 9)), width, height);
        labelJ.setFont(font);

        frame.getContentPane().add(labelA);
        frame.getContentPane().add(labelB);
        frame.getContentPane().add(labelC);
        frame.getContentPane().add(labelD);
        frame.getContentPane().add(labelE);
        frame.getContentPane().add(labelF);
        frame.getContentPane().add(labelG);
        frame.getContentPane().add(labelH);
        frame.getContentPane().add(labelI);
        frame.getContentPane().add(labelJ);
    }

    public static void editTextSerial(JFrame frame) {
        SerialPostDto serialPostDto = SerialPostDto.getInstance();

        // x
        int labelX = (int)Math.abs(frame.getBounds().getX() * 0.0333);
        int fieldX = (int)Math.abs(frame.getBounds().getX() * 0.125);
        int y = (int)Math.abs(frame.getBounds().getY() * 0.222);
        int height = (int)Math.abs(frame.getBounds().getHeight() * 0.084);
        int interval = (int) Math.abs(frame.getBounds().getWidth() * 0.083);
        int width = (int) Math.abs(frame.getBounds().getWidth() * 0.148);
        int width2 = (int) Math.abs(frame.getBounds().getWidth() * 0.593);

        JLabel serialNumberLabel = new JLabel("串口号");
        serialNumberLabel.setBounds(labelX, y, width, height);
        serialNumberField = new JTextField();
        serialNumberField.setText(serialPostDto.getSerialNumber());
        serialNumberField.setBounds(fieldX, y, width2, height);
        frame.add(serialNumberLabel);
        frame.add(serialNumberField);

        JLabel baudRateLabel = new JLabel("波特率");
        baudRateLabel.setBounds(labelX, y * 2 + interval, width, height);
        baudRateField = new JTextField();
        baudRateField.setText(String.valueOf(serialPostDto.getBaudRate()));
        baudRateField.setBounds(fieldX, y * 2 + interval, width2, height);
        frame.add(baudRateLabel);
        frame.add(baudRateField);

        JLabel checkoutBitLabel = new JLabel("校验位");
        checkoutBitLabel.setBounds(labelX, y * 3 + (interval * 2), width, height);
        checkoutBitField = new JTextField();
        checkoutBitField.setText(String.valueOf(serialPostDto.getCheckoutBit()));
        checkoutBitField.setBounds(fieldX, y * 3 + (interval * 2), width2, height);
        frame.add(checkoutBitLabel);
        frame.add(checkoutBitField);

        JLabel dataBitLabel = new JLabel("数据位");
        dataBitLabel.setBounds(labelX, y * 4 + (interval * 3), width, height);
        dataBitField = new JTextField();
        dataBitField.setText(String.valueOf(serialPostDto.getDataBit()));
        dataBitField.setBounds(fieldX, y * 4 + (interval * 3), width2, height);
        frame.add(dataBitLabel);
        frame.add(dataBitField);

        JLabel stopBitLabel = new JLabel("停止位");
        stopBitLabel.setBounds(labelX, y * 5 + (interval * 4), width, height);
        stopBitField = new JTextField();
        stopBitField.setText(String.valueOf(serialPostDto.getStopBit()));
        stopBitField.setBounds(fieldX, y * 5 + + (interval * 4), width2, height);
        frame.add(stopBitLabel);
        frame.add(stopBitField);
    }

    public static Map<String, String> getSerialMap() {
        Map<String, String> serialMap = new HashMap<>();
        serialMap.put("serialNumber", serialNumberField.getText());
        serialMap.put("baudRate", baudRateField.getText().trim());
        serialMap.put("checkoutBit", checkoutBitField.getText());
        serialMap.put("dataBit", dataBitField.getText());
        serialMap.put("stopBit", stopBitField.getText());
        return serialMap;
    }
}
